package com.example.onlineanswer.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.onlineanswer.model.entity.Contest;
import com.example.onlineanswer.model.vo.ContestVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
 * 竞赛Mapper接口
 */
@Mapper
public interface ContestMapper extends BaseMapper<Contest> {

    /**
     * 分页查询竞赛列表（包含创建者信息）
     *
     * @param page          分页参数
     * @param status        状态（可选）
     * @param excludeStatus 排除的状态（可选）
     * @param keyword       关键词（可选）
     * @param creatorId     创建者ID（可选）
     * @return 竞赛列表
     */
    @Select("<script>" +
            "SELECT c.*, u.nickname as creator_name, ru.nickname as reviewer_name " +
            "FROM contest c " +
            "LEFT JOIN sys_user u ON c.creator_id = u.id " +
            "LEFT JOIN sys_user ru ON c.reviewer_id = ru.id " +
            "WHERE c.is_deleted = 0 " +
            "<if test='status != null'> AND c.status = #{status} </if> " +
            "<if test='excludeStatus != null'> AND c.status != #{excludeStatus} </if> " +
            "<if test='keyword != null and keyword != \"\"'> AND c.title LIKE CONCAT('%', #{keyword}, '%') </if> " +
            "<if test='creatorId != null'> AND c.creator_id = #{creatorId} </if> " +
            "ORDER BY c.create_time DESC" +
            "</script>")
    IPage<ContestVO> pageContestList(Page<ContestVO> page, 
                                     @Param("status") Integer status,
                                     @Param("excludeStatus") Integer excludeStatus,
                                     @Param("keyword") String keyword, 
                                     @Param("creatorId") Long creatorId);

    /**
     * 根据ID获取竞赛详情（包含创建者信息）
     *
     * @param id 竞赛ID
     * @return 竞赛详情
     */
    @Select("SELECT c.*, u.nickname as creator_name, ru.nickname as reviewer_name " +
           "FROM contest c " +
           "LEFT JOIN sys_user u ON c.creator_id = u.id " +
           "LEFT JOIN sys_user ru ON c.reviewer_id = ru.id " +
           "WHERE c.id = #{id} AND c.is_deleted = 0")
    ContestVO getContestDetailById(@Param("id") Long id);

    /**
     * 更新竞赛参与人数
     *
     * @param id                竞赛ID
     * @param incrementValue   增加的值
     * @return 影响行数
     */
    @Update("UPDATE contest SET current_participants = current_participants + #{incrementValue} " +
           "WHERE id = #{id} AND " +
           "(participant_limit IS NULL OR current_participants + #{incrementValue} <= participant_limit)")
    int updateParticipants(@Param("id") Long id, @Param("incrementValue") int incrementValue);

    /**
     * 更新当前题目ID
     *
     * @param id              竞赛ID
     * @param currentQuestionId 当前题目ID
     * @return 影响行数
     */
    @Update("UPDATE contest SET current_question_id = #{currentQuestionId} WHERE id = #{id}")
    int updateCurrentQuestionId(@Param("id") Long id, @Param("currentQuestionId") Long currentQuestionId);

    /**
     * 自动更新状态为已结束（针对已过结束时间的竞赛）
     *
     * @return 影响行数
     */
    @Update("UPDATE contest SET status = 4 WHERE status = 1 AND end_time < NOW()")
    int autoUpdateEndedStatus();
} 